package com.winning.cqs.pregnantbabycare_cqs_v1.table;

import java.io.Serializable;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.winning.cqs.pregnantbabycare_cqs_v1.entity.FriendTalkListResult;
import com.winning.cqs.pregnantbabycare_cqs_v1.table.Talk.TalkTable;
import com.winning.cqs.pregnantbabycare_cqs_v1.utils.Constants;

public class Friend implements Serializable {

	private static final long serialVersionUID = 1L;

	public String friend_id;

	public String name;

	public String image_url;

	public String user_id;

	public boolean current_user = false;

	public static void saveFriendToDB(ContentResolver cr, Friend friend) {
		if (null == cr || null == friend) {
			return;
		}

		ContentValues values = new ContentValues();

		values.put(FriendTable.FRIEND_ID, friend.friend_id);
		values.put(FriendTable.IMAGE_URL, friend.image_url);
		values.put(FriendTable.NAME, friend.name);

		values.put(FriendTable.USER_ID, friend.user_id);

		if (values.size() > 0) {
			String whereStr = FriendTable.FRIEND_ID + " = " + friend.friend_id;
			Cursor cursor = cr.query(FriendTable.CONTENT_URI, null, whereStr,
					null, null);
			if ((null != cursor) && (0 < cursor.getCount())) {
				cr.update(FriendTable.CONTENT_URI, values, whereStr, null);
			} else {
				cr.insert(FriendTable.CONTENT_URI, values);
			}
			if (cursor != null) {
				cursor.close();
			}
		}
	}

	public static ArrayList<Friend> getAllMyFriends(ContentResolver cr) {
		ArrayList<Friend> friends = new ArrayList<Friend>();
		if (null != cr) {
			String whereStr = FriendTable.FRIEND_ID + " <> " + "1001";

			Cursor cursor = cr.query(FriendTable.CONTENT_URI, null, whereStr,
					null, null);
			if (null != cursor) {
				int friendsCount = cursor.getCount();

				cursor.moveToFirst();
				for (int k = 0; k < friendsCount; k++) {
					Friend friend = new Friend();
					friend.friend_id = cursor.getString(cursor
							.getColumnIndex(FriendTable.FRIEND_ID));
					friend.image_url = cursor.getString(cursor
							.getColumnIndex(FriendTable.IMAGE_URL));
					friend.name = cursor.getString(cursor
							.getColumnIndex(FriendTable.NAME));
					friend.user_id = cursor.getString(cursor
							.getColumnIndex(FriendTable.USER_ID));

					friends.add(friend);

					cursor.moveToNext();
				}
				cursor.close();
			}

		}
		return friends;
	}

	public static Friend getFriendById(ContentResolver cr, String friend_id) {
		Friend friend = null;
		if (null != cr) {
			String whereStr = FriendTable.FRIEND_ID + " = " + friend_id;

			Cursor cursor = cr.query(FriendTable.CONTENT_URI, null, whereStr,
					null, null);
			if (null != cursor && cursor.getCount() > 0) {

				cursor.moveToFirst();
				friend = new Friend();
				friend.friend_id = cursor.getString(cursor
						.getColumnIndex(FriendTable.FRIEND_ID));
				friend.image_url = cursor.getString(cursor
						.getColumnIndex(FriendTable.IMAGE_URL));
				friend.name = cursor.getString(cursor
						.getColumnIndex(FriendTable.NAME));
				friend.user_id = cursor.getString(cursor
						.getColumnIndex(FriendTable.USER_ID));

				cursor.close();
			}

		}
		return friend;
	}

	public static ArrayList<FriendTalkListResult> getTalkList(
			ContentResolver cr, int offset_num) {
		ArrayList<FriendTalkListResult> friendTalkList = new ArrayList<FriendTalkListResult>();

		if (null != cr) {
			int limit_num = offset_num + 1;
			Cursor cursor = cr.query(TalkTable.CONTENT_URI, null, null, null,
					"date desc limit " + (15 * limit_num) + " offset  "
							+ (offset_num * 15));
			if (null != cursor && cursor.getCount() > 0) {
				int talkCount = cursor.getCount();
				cursor.moveToFirst();

				for (int k = 0; k < talkCount; k++) {
					FriendTalkListResult result = new FriendTalkListResult();

					result.talk_id = cursor.getString(0);
					result.talk_content = cursor
							.getString(TalkTable.CONTENT_INDEX);
					result.talk_date = cursor.getString(TalkTable.DATE_INDEX);
					result.friend_id = cursor
							.getString(TalkTable.USER_ID_INDEX);

					result.is_share = cursor
							.getString(TalkTable.IS_SHARE_INDEX);
					result.share_image = cursor
							.getString(TalkTable.SHARE_IMAGE_INDEX);
					result.share_title = cursor
							.getString(TalkTable.SHARE_TITLE_INDEX);

					result.share_url = cursor
							.getString(TalkTable.SHARE_URL_INDEX);

					Friend friend = Friend.getFriendById(cr, result.friend_id);

					if (friend != null) {
						result.friend_name = friend.name;
						result.friend_image = friend.image_url;
						result.commentlist = Comment.queryCommentsByTalkId(cr,
								result.talk_id);

						friendTalkList.add(result);
					}
					cursor.moveToNext();
				}
			}
			cursor.close();

		}
		return friendTalkList;

	}

	public static class FriendTable implements BaseColumns {

		public static final String[] FRIEND_PROJECTION = { FriendTable._ID,
				FriendTable.USER_ID, FriendTable.NAME, FriendTable.IMAGE_URL,
				FriendTable.FRIEND_ID

		};

		public static final String TABLE_NAME = "friend";

		public static final Uri CONTENT_URI = Uri.parse("content://"
				+ Constants.AUTHORITY + "/" + TABLE_NAME);

		public static final String CONTENT_TYPE = "vnd.android.cursor.dir/"
				+ Constants.AUTHORITY + "/" + TABLE_NAME;

		public static final String USER_ID = "user_id";

		public static final String NAME = "name";

		public static final String IMAGE_URL = "image_url";

		public static final String FRIEND_ID = "friend_id";

		public static final String TABLE_CREATESQL = "CREATE TABLE "
				+ TABLE_NAME + " (" + "_id"
				+ " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + USER_ID
				+ " TEXT," + NAME + " TEXT," + IMAGE_URL + " TEXT," + FRIEND_ID
				+ " TEXT" + ");";

	}

}
